package com.base.cn.platform.os.service.order.check;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.activity.OpActivity;
import com.base.cn.platform.os.entity.op.coupon.OpCouponCode;
import com.base.cn.platform.os.entity.order.order.Order;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.service.order.price.OrderPrice;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建订单数据对象
 *
 * @author s.li
 * @date 2019/05/27
 */
@Data
public class CreateOrderDataFrom {
    /**观察者对象集合*/
    private List<OrderCheckObserver> observerList = new ArrayList<>(3);
    /**是否是支付类型订单，true是，false否*/
    private boolean isPay;
    /**订单类型，COMMON普通订单，ACCOUNT充值订单*/
    private String orderType;
    /**支付类型*/
    private String payType;
    /**用户ID*/
    private BigDecimal userId;
    /**商品数据，数据格式：shopId-shopType-buyNum-,shopId-shopType-buyNum*/
    private String shopData;
    /**购物车ID串*/
    private String shopCartIds;
    /**收货地址ID*/
    private BigDecimal addressId;
    /**收货地址*/
    private String address;
    /**优惠券编号*/
    private String code;
    /**活动ID*/
    private BigDecimal activityId;
    /**分享人的KEY*/
    private String shareKey;
    /**活动对象*/
    private OpActivity activity;
    /**活动查询状态*/
    private boolean queryActivity;
    /**优惠券编码对象*/
    private OpCouponCode couponCode;
    /**优惠查询状态*/
    private boolean queryCouponCode;
    /**商品列表*/
    private List<ShopData> shopList;
    /**订单来源*/
    private String orderForm;
    /**是否是提交创建订单*/
    private boolean submit;
    /**是否是免费订单*/
    private boolean free;
    /**是否是重新验证*/
    private boolean checkAgain;
    /**商品数量*/
    private int shopNum;
    /**是否是会员用户*/
    private boolean memberUser;
    /**是否是系统自动赠送*/
    private boolean autoGive;
    /**售卖方式*/
    private String selaSwitch;
    /**订单对象*/
    private Order order;
    /**
     * 订单信息
     */
    private  List<Map<String,String>>  paramsList;
    /**
     * 订单价格对象
     */
    private OrderPrice orderPrice;
    /**
     *是否拥有课程
     */
   private boolean haveBook;

    /**
     * 添加观察者对象
     * @param observer
     */
    public void addObserver(OrderCheckObserver observer){
        observerList.add(observer);
    }

    /**
     * 清空观察煮数据
     */
    public void clear(){
        observerList.clear();
    }

    /**
     * 复制商品数据列表
     * @return List<CouponShopData>
     */
    public List<ShopData> cloneShopDataList(){
        List<ShopData> _shopDataList = new ArrayList<>();
        this.getShopList().forEach(shop->{
            _shopDataList.add(shop.clone());
        });
        return _shopDataList;
    }

    /**
     * 通知观察对象
     * @return Map<String,Object>
     */
    public Map<String,Object> notifyAllObservers(boolean reset){
        if(ObjectUtils.isNotEmpty(observerList)){
            if(observerList.size() ==3){
                OrderCheckObserver observer = observerList.get(0);
                Map<String,Object> resultMap = observer.orderCheck();
                if(!ResultUtil.getSuccess(resultMap)){
                    return resultMap;
                }
                if(!reset){
                    for (int i = 1; i < 3; i++) {
                        observer = observerList.get(i);
                        resultMap = observer.orderCheck();
                        if(!ResultUtil.getSuccess(resultMap)){
                            return resultMap;
                        }
                    }
                }else{
                    for (int i=2 ; i > 0 ; i --){
                        observer = observerList.get(i);
                        resultMap = observer.orderCheck();
                        if(!ResultUtil.getSuccess(resultMap)){
                            return resultMap;
                        }
                    }
                }
            }else{
                for(OrderCheckObserver observer: observerList){
                    Map<String,Object> resultMap = observer.orderCheck();
                    if(!ResultUtil.getSuccess(resultMap)){
                        return resultMap;
                    }
                }
            }
        }else{
            return ResultUtil.ERROR("没有商品数据，订单创建失败");
        }
        Map<String,Object> resultMap = ResultUtil.SUCCESS(this);
        List<ShopData> shopDataList = this.getShopList();
        List<Map<String,String>> paramsList = new ArrayList<>();
        shopDataList.forEach(e->{
            Map<String,String> map = new HashMap<>();
            map.put("shopType",e.getShopType());
            map.put("shopId",e.getShopId().toString());
            map.put("price",String.valueOf(e.getRealPrice()));
            map.put("buyNum",String.valueOf(e.getBuyNum()));
            paramsList.add(map);
        });
        this.setParamsList(paramsList);
        return resultMap;
    }
}
